import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout, QLineEdit, QPushButton
from PyQt5.QtGui import QMovie, QFont
from PyQt5.QtCore import Qt, QTimer, QTime


class LoginWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.init_ui()

    def init_ui(self):
        # 窗口基本属性
        self.setWindowTitle('登录界面')
        self.setGeometry(200, 200, 800, 600)
        
        # 设置GIF背景
        self.set_background_gif("background.gif")

        # 顶部显示当前时间的Label
        self.time_label = QLabel(self)
        self.time_label.setFont(QFont("Arial", 14))
        self.time_label.setStyleSheet("color: white;")
        self.time_label.move(20, 20)  # 距离左边20px
        self.update_time()
        
        # 使用 QTimer 实现时间自动更新
        timer = QTimer(self)
        timer.timeout.connect(self.update_time)
        timer.start(1000)  # 每秒更新

        # 界面布局
        self.username_input = QLineEdit(self)
        self.username_input.setPlaceholderText("用户名")
        self.username_input.setStyleSheet("background-color: rgba(255, 255, 255, 200);")
        self.password_input = QLineEdit(self)
        self.password_input.setEchoMode(QLineEdit.Password)
        self.password_input.setPlaceholderText("密码")
        self.password_input.setStyleSheet("background-color: rgba(255, 255, 255, 200);")

        self.login_button = QPushButton("登录", self)
        self.login_button.setStyleSheet("background-color: green; color: white;")

        # 布局管理
        layout = QVBoxLayout(self)
        layout.addWidget(self.time_label, alignment=Qt.AlignTop | Qt.AlignLeft)
        layout.addWidget(self.username_input)
        layout.addWidget(self.password_input)
        layout.addWidget(self.login_button)

        self.setLayout(layout)

    def set_background_gif(self, gif_path):
        # 设置GIF背景
        self.background_label = QLabel(self)
        self.background_label.setGeometry(0, 0, 800, 600)  # 需要与窗口尺寸匹配
        self.movie = QMovie(gif_path)
        self.background_label.setMovie(self.movie)
        self.movie.start()
        self.background_label.lower()  # 将背景放在最底层

    def update_time(self):
        current_time = QTime.currentTime().toString("HH:mm:ss")
        self.time_label.setText(f"当前时间：{current_time}")


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = LoginWindow()
    window.show()
    sys.exit(app.exec_())
